package a_o2odata_deal.utils.zhibo_zf.zhengli

import a_aa_amainpackage.a_o2odata_deal.config.config._
import org.apache.spark.sql.SQLContext

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/2/11 11:10
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object handle_shop {
  def handle_shop(sqlContext:SQLContext):Unit={
    //加载天猫 淘宝数据
    val tb_data = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/taobao/${years}/${months}/zhibo_finally")
      .selectExpr("platformId","shopId","is_showLive","timeStamp")
    val tm_data = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/${years}/${months}/zhibo_finally")
      .selectExpr("platformId","shopId","is_showLive","timeStamp")

    tb_data.createOrReplaceTempView("tb_data")
    tm_data.createOrReplaceTempView("tm_data")

    sqlContext.udf.register("handle_is_showLive",handle_is_showLive _)
    //只有一个店铺只有一个直播商品就算直播店铺，否则就不是直播店铺
    val tb_result = sqlContext.sql(
      s"""
         |select
         |platformId,
         |timeStamp,
         |shopId,
         |handle_is_showLive(is_showLive_collect) as is_liveShop
         |from
         |(select
         |platformId,
         |timeStamp,
         |shopId,
         |collect_list(is_showLive) as is_showLive_collect
         |from tb_data
         |group by platformId,timeStamp,shopId)
         """.stripMargin)

    val tm_result = sqlContext.sql(
      s"""
         |select
         |platformId,
         |timeStamp,
         |shopId,
         |handle_is_showLive(is_showLive_collect) as is_liveShop
         |from
         |(select
         |platformId,
         |timeStamp,
         |shopId,
         |collect_list(is_showLive) as is_showLive_collect
         |from tm_data
         |group by platformId,timeStamp,shopId)
         """.stripMargin)

    tb_result.selectExpr("platformId","timeStamp","shopId","is_liveShop").union(
      tm_result.selectExpr("platformId","timeStamp","shopId","is_liveShop")
    ).repartition(12).write.orc(s"s3a://o2o-dataproces-group/panzonghao/zhibo_zf/${years}/${months}/is_liveShop/")
  }

  import scala.util.control.Breaks.{break, breakable}
  def handle_is_showLive(is_showLive_collect:Seq[String]):String={
    var b = "false"
    breakable {
      for (i <- 0 to is_showLive_collect.length-1){
        if (is_showLive_collect(i)=="true"){
          b = "true"
          break
        }
      }
    }

    b
  }

}
